<?php


namespace app\common\model;


use think\Db;
use think\Model;

class User5g
{

    public static function reg5gUser($user)
    {
        if(input('user_type') == 1 && isset($user['user_type'])){
            Db::name('user')->where('id',$user['id'])->update(['user_type' => 1]);
        }
        //$user['mobile'] = '13136281919';
        //print_r($config);die();
        $domain = request()->domain();
        $zhong_tai = config('db_zhongtai');
        $site = Db::connect($zhong_tai)
            ->table('app_manage')->where('url',$domain)->find();
        $site_id = $site?$site['id']:0;

        $config = config('db_online5g');
        $user5g = Db::connect($config)
            ->table('fa_user')
            ->where('mobile',$user['mobile'])
            ->where('site_id',$site_id)
            ->find();
        log_print('find_sql');
        log_print(Db::connect($config)
            ->table('fa_user')->getLastSql());
        log_print('user5g',$user5g);
        if(empty($user5g)){
            $data = [
                'username' => $user['username'],
                'nickname' => $user['nickname'],
                'mobile' => $user['mobile'],
                'password' => $user['password'],
                'salt' => $user['salt'],
                'site_id' => $site_id,
                'is_app' => 1,
            ];
            $user_id = Db::connect($config)
                ->table('fa_user')
                ->insertGetId($data);
            log_print('AAA',$user_id);
        }else{
            $data = [
                'username' => $user['username'],
                'nickname' => $user['nickname'],
                'mobile' => $user['mobile'],
                'password' => $user['password'],
                'salt' => $user['salt'],
                'site_id' => $site_id,
                'is_app' => 1,
            ];
            $user_id = $user5g['id'];
            Db::connect($config)
                ->table('fa_user')->where('id',$user_id)->update($data);
            log_print('BBB',$user_id);
        }
        $token = Db::table('user_token')
            ->where('user_id',$user['id'])
            ->order('createtime desc')->find();
        log_print('同步token');
        log_print($token);
        log_print('user_id',$user_id);
        if($token){
            $insert = [
                'token' => $token['token'],
                'user_id' => $user_id,
                'createtime' => $token['createtime'],
                'expiretime' => $token['expiretime'],
            ];
            Db::connect($config)
                ->table('fa_user_token')
                ->insert($insert);
            log_print('sql--');
            log_print(Db::connect($config)
                ->table('fa_user_token')->getLastSql());
        }
    }
    public static function reg5gUserUp($user)
    {
        //$user['mobile'] = '13136281919';
        //print_r($config);die();
        $domain = request()->domain();
        $zhong_tai = config('db_zhongtai');
        $site = Db::connect($zhong_tai)
            ->table('app_manage')->where('url',$domain)->find();
        $site_id = $site?$site['id']:0;

        $config = config('db_online5g');
        $user5g = Db::connect($config)
            ->table('fa_user')
            ->where('mobile',$user['mobile'])
            ->where('site_id',$site_id)
            ->find();
        log_print('find_sql');
        log_print(Db::connect($config)
            ->table('fa_user')->getLastSql());
        log_print('user5g',$user5g);
        if(empty($user5g)){
            $data = [
                'username' => $user['username'],
                'nickname' => $user['nickname'],
                'mobile' => $user['mobile'],
                'password' => $user['password'],
                'salt' => $user['salt'],
                'site_id' => $site_id
            ];
            $user_id = Db::connect($config)
                ->table('fa_user')
                ->insertGetId($data);
            log_print('AAA',$user_id);
        }else{
            $data = [
                'username' => $user['username'],
                'nickname' => $user['nickname'],
                'mobile' => $user['mobile'],
                'password' => $user['password'],
                'salt' => $user['salt'],
                'site_id' => $site_id
            ];
            $user_id = $user5g['id'];
            Db::connect($config)
                ->table('fa_user')->where('id',$user_id)->update($data);
            log_print('BBB',$user_id);
        }
        $token = Db::table('user_token')
            ->where('user_id',$user['id'])
            ->order('createtime desc')->find();
        log_print('同步token');
        log_print($token);
        log_print('user_id',$user_id);
        if($token){
            $token_data = Db::connect($config)
                ->table('fa_user_token')->where(['token'=>$token['token']])->find();
            if(!$token_data){
                $insert = [
                    'token' => $token['token'],
                    'user_id' => $user_id,
                    'createtime' => $token['createtime'],
                    'expiretime' => $token['expiretime'],
                ];
                Db::connect($config)
                    ->table('fa_user_token')
                    ->insert($insert);
            }

            log_print('sql--');
            log_print(Db::connect($config)
                ->table('fa_user_token')->getLastSql());
        }
    }
}
